﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SStuff.FlowControls
{
    public class CoverFlow2D : IFlowLayout2DAlgorithm
    {
        Size _availableSize;
        #region IFluidLayout2DAlgorithm Members

        public void SetAvailableSize(Size availableSize)
        {
            _availableSize = availableSize;
        }

        public int GetMaxDisplayedItems()
        {
            return 11;
        }
        EasingFunctionBase _easer = new PowerEase { EasingMode = EasingMode.EaseOut };

        public void ComputeLayout(double normalizedOffset, IFlowItem2D item, int consideredItemsCount)
        {
            var absOffset = Math.Abs(normalizedOffset);
            var easedOffset = _easer.Ease(absOffset);
            if (normalizedOffset < 0)
                easedOffset = -easedOffset;
            item.Transform2D.TranslateX = easedOffset * _availableSize.Width * .4;
            item.RelativeZIndex = -absOffset;

            var scale = 1 -_easer.Ease(absOffset) * 0.7;
            item.Transform2D.ScaleX = scale;
            item.Transform2D.ScaleY = scale;
            //if (absOffset > 0.1)
            //{
            //    var scale = 1 - 0.7 * absOffset;
            //    item.Transform2D.ScaleX = scale;
            //    item.Transform2D.ScaleY = scale;
            //}
            //else
            //{
            //    var scale = 0.93;
            //    var offset = (1 - absOffset * 10) * 0.3;
            //    scale += offset;
            //    item.Transform2D.ScaleX = scale;
            //    item.Transform2D.ScaleY = scale;
            //}
            item.RelativeZIndex = -absOffset;
            if (absOffset > 0.9)
            {
                item.Opacity = 1 - (absOffset - 0.9) * 10;
            }
            else
            {
                item.Opacity = 1;
            }
        }

        public bool SupportsGestures
        {
            get { return true; }
        }

        public double GetNormalizedOffsetAtPoint(Point contactPosition)
        {
            double halfWidth = 0.5 * this._availableSize.Width;
            double x = contactPosition.X - halfWidth;
            return x / halfWidth;
        }

        #endregion
    }
}
